/**
 * 文件名：FileUtil.java
 *
 * 创建人：[马佳] - [jia_ma@asdc.com.cn] // 根据个人情况在模板中进行修改
 *
 * 创建时间：2012-9-17 下午05:09:09
 *
 * 版权所有：IBM
 */
package com.ibm.moa.outmail.utils;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Date;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.text.TextUtils;
import android.webkit.MimeTypeMap;
import android.widget.Toast;

import com.ibm.moa.outmail.MailApplication;

/**
 * <p>
 * Title: [名称]
 * </p>
 * <p>
 * Description: [描述]
 * </p>
 * <p>
 * Created on 2012-9-17
 * </p>
 * <p>
 * Copyright: Copyright (c) 2012
 * </p>
 * <p>
 * Company: IBM
 * </p>
 * 
 * @author [张役兵]
 * @version 1.0
 */
public class FileUtil {

	/**
	 * 调用系统方法打开指定路径的文件
	 * 
	 * @param context
	 * @param filePath
	 */
	public static void openFile(Context context, String filePath) {
		if (!TextUtils.isEmpty(filePath)) {
			try {
				context.startActivity(getAllIntent(filePath));
			} catch (Exception e) {
				Toast.makeText(context, "无法打开此文件！\n" + filePath, Toast.LENGTH_SHORT).show();
			}
		} else {
			// Toast.makeText(context, "无法打开此文件！", Toast.LENGTH_SHORT).show();
		}
	}

	public static Intent getAllIntent(String path) {
		Intent intent = new Intent();
		intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		intent.setAction(android.content.Intent.ACTION_VIEW);
		intent.addCategory("android.intent.category.DEFAULT");
		Uri uri = Uri.fromFile(new File(path));
		intent.setDataAndType(uri, getMimeType(path));
		Toast.makeText(MailApplication.getInstance(), path, 0).show();
		return intent;
	}

	/**
	 * 得到指定文件的MIME-type
	 * 
	 * @param file
	 * @return
	 */
	public static String getMimeType(File file) {
		MimeTypeMap map = MimeTypeMap.getSingleton();
		String fileName = file.getAbsolutePath();
		int index = fileName.lastIndexOf(".");
		if (index < 0) {
			return null;
		}
		return map.getMimeTypeFromExtension(fileName.substring(index + 1));
	}

	/**
	 * 得到指定文件的MIME-type
	 * 
	 * @param fileName
	 * @return
	 */
	public static String getMimeType(String fileName) {
		MimeTypeMap map = MimeTypeMap.getSingleton();
		if (fileName == null) {
			return null;
		}
		int index = fileName.lastIndexOf(".");
		if (index < 0) {
			return null;
		}
		String mimeType = map.getMimeTypeFromExtension(fileName.substring(index + 1));
		if (mimeType != null && mimeType.startsWith("text/")) {
			mimeType = "text/*";
		}
		return mimeType;
	}

	/**
	 * 根据byte数组，生成文件
	 */
	public static String getFile(byte[] bfile, String filePath, String fileName) {
		BufferedOutputStream bos = null;
		FileOutputStream fos = null;
		File file = null;
		// 重命名附件
//		Long csurrtime = new Date().getTime();
//		String fileType = "";
//		if (fileName.contains(".")) {
//			fileType = fileName.substring(fileName.lastIndexOf("."), fileName.length());
//		}
//		String name = csurrtime + fileType;
		String name = fileName;
		try {
			File dir = new File(filePath);
			if (!dir.exists()) {// 判断文件目录是否存在
				dir.mkdirs();
			}
			file = new File(filePath + "/" + name);
			fos = new FileOutputStream(file);
			bos = new BufferedOutputStream(fos);
			bos.write(bfile);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (bos != null) {
				try {
					bos.close();
				} catch (IOException e1) {
					e1.printStackTrace();
				}
			}
			if (fos != null) {
				try {
					fos.close();
				} catch (IOException e1) {
					e1.printStackTrace();
				}
			}
		}
		return name;
	}

	/**
	 * 根据byte数组，生成文件 转发生成文件使用
	 */
	public static String getFile2(byte[] bfile, String filePath, String fileName) {
		BufferedOutputStream bos = null;
		FileOutputStream fos = null;
		File file = null;
		String fileURL = null;
		// 重命名附件
		Long csurrtime = new Date().getTime();
		String fileType = fileName.substring(fileName.lastIndexOf("."), fileName.length());
		String name = csurrtime + fileType;
		try {
			File dir = new File(filePath);
			if (!dir.exists()) {// 判断文件目录是否存在
				dir.mkdirs();
			}
			fileURL = filePath + "/" + name;
			file = new File(fileURL);
			fos = new FileOutputStream(file);
			bos = new BufferedOutputStream(fos);
			bos.write(bfile);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (bos != null) {
				try {
					bos.close();
				} catch (IOException e1) {
					e1.printStackTrace();
				}
			}
			if (fos != null) {
				try {
					fos.close();
				} catch (IOException e1) {
					e1.printStackTrace();
				}
			}
		}
		return fileURL;
	}

	/**
	 * 删除文件夹
	 * 
	 * @param filePathAndName
	 *            String 文件夹路径及名称 如c:/fqf
	 * @param fileContent
	 *            String
	 * @return boolean
	 */
	public static void delFolder(String folderPath) {
		try {
			delAllFile(folderPath); // 删除完里面所有内容
			String filePath = folderPath;
			filePath = filePath.toString();
			java.io.File myFilePath = new java.io.File(filePath);
			myFilePath.delete(); // 删除空文件夹

		} catch (Exception e) {
			System.out.println("删除文件夹操作出错");
			e.printStackTrace();

		}

	}

	/**
	 * 删除文件夹里面的所有文件
	 * 
	 * @param path
	 *            String 文件夹路径 如
	 */
	private static void delAllFile(String path) {
		File file = new File(path);
		if (!file.exists()) {
			return;
		}
		if (!file.isDirectory()) {
			return;
		}
		String[] tempList = file.list();
		File temp = null;
		for (int i = 0; i < tempList.length; i++) {
			if (path.endsWith(File.separator)) {
				temp = new File(path + tempList[i]);
			} else {
				temp = new File(path + File.separator + tempList[i]);
			}
			if (temp.isFile()) {
				temp.delete();
				System.out.println("删除文件:" + temp.getName());
			}
		}
	}
}
